package campus.service.impl;

import campus.domain.CampusVipLevel;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import campus.repository.CampusVipLevelRepository;
import campus.service.CampusVipLevelService;
import campus.service.dto.CampusVipLevelDto;
import campus.service.dto.CampusVipLevelQueryCriteria;
import campus.service.mapper.CampusVipLevelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 李诗莹
* @date 2020-03-05
*/
@Service
//@CacheConfig(cacheNames = "campusVipLevel")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CampusVipLevelServiceImpl implements CampusVipLevelService {

    private final CampusVipLevelRepository campusVipLevelRepository;

    private final CampusVipLevelMapper campusVipLevelMapper;

    public CampusVipLevelServiceImpl(CampusVipLevelRepository campusVipLevelRepository, CampusVipLevelMapper campusVipLevelMapper) {
        this.campusVipLevelRepository = campusVipLevelRepository;
        this.campusVipLevelMapper = campusVipLevelMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CampusVipLevelQueryCriteria criteria, Pageable pageable){
        Page<CampusVipLevel> page = campusVipLevelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(campusVipLevelMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CampusVipLevelDto> queryAll(CampusVipLevelQueryCriteria criteria){
        return campusVipLevelMapper.toDto(campusVipLevelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CampusVipLevelDto findById(Integer id) {
        CampusVipLevel campusVipLevel = campusVipLevelRepository.findById(id).orElseGet(CampusVipLevel::new);
        ValidationUtil.isNull(campusVipLevel.getId(),"CampusVipLevel","id",id);
        return campusVipLevelMapper.toDto(campusVipLevel);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CampusVipLevelDto create(CampusVipLevel resources) {
        return campusVipLevelMapper.toDto(campusVipLevelRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CampusVipLevel resources) {
        CampusVipLevel campusVipLevel = campusVipLevelRepository.findById(resources.getId()).orElseGet(CampusVipLevel::new);
        ValidationUtil.isNull( campusVipLevel.getId(),"CampusVipLevel","id",resources.getId());
        campusVipLevel.copy(resources);
        campusVipLevelRepository.save(campusVipLevel);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            campusVipLevelRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CampusVipLevelDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CampusVipLevelDto campusVipLevel : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("等级名称", campusVipLevel.getLevelName());
            map.put("权限（	VIP详情	）", campusVipLevel.getPermissions());
            map.put("0-使用，1-删除（状态）", campusVipLevel.getState());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}